package com.autumn.demo.services.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.autumn.common.api.application.dto.input.DefaultPrimaryKeyInput;
import com.autumn.common.api.application.dto.input.PageQueryInput;
import com.autumn.common.api.application.dto.output.IPageQueryResult;
import com.autumn.common.api.application.dto.output.PageQueryResult;
import com.autumn.demo.dto.input.NameQueryInput;
import com.autumn.demo.dto.output.BasicsInfoOutput;
import com.autumn.demo.entities.ds1.Duties;
import com.autumn.demo.entities.ds2.Nation;
import com.autumn.demo.repositories.ds1.IDutiesRepository;
import com.autumn.demo.repositories.ds2.INationRepository;
import com.autumn.demo.services.IDemoDataBaseService;
import com.autumn.mybatis.criterion.IQuerySection;
import com.autumn.mybatis.criterion.LogicalOperatorEnum;
import com.autumn.mybatis.criterion.Query;
import com.autumn.util.AutoMapUtils;
import com.autumn.util.DataRollbackException;
import com.autumn.util.function.FunctionOneAction;

/**
 * 
 * @author 老码农
 *
 *         2018-01-08 12:42:19
 */
@Service
public class DemoDataBaseServiceImpl implements IDemoDataBaseService {

	@Autowired
	private IDutiesRepository dutiesRepository;

	@Autowired
	private INationRepository nationRepository;

	/**
	 * 获取职务信息
	 * 
	 * @param input
	 *            输入
	 * @return
	 * @author 杨昌国 2017-12-01 17:08:41
	 */
	@Override
	public BasicsInfoOutput getDutiesInfo(DefaultPrimaryKeyInput input) {
		Duties info = dutiesRepository.get(input.getId());
		return AutoMapUtils.map(info, BasicsInfoOutput.class);
	}

	@Override
	public List<BasicsInfoOutput> dutiesList() {
		List<Duties> dutiesLst = dutiesRepository.selectAll();
		return AutoMapUtils.mapList(dutiesLst, BasicsInfoOutput.class);
	}

	@Override
	@Transactional
	public List<BasicsInfoOutput> nationList() {
		List<Nation> nationLst = nationRepository.selectAll();
		return AutoMapUtils.mapList(nationLst, BasicsInfoOutput.class);
	}

	@Override
	public BasicsInfoOutput getNationInfo(NameQueryInput input) {
		Query query = new Query(Nation.class);
		query.like("name", input.getName()).orderBy("sortId");
		// query.eq(name, value)
		// query.le(name, value)

		Nation nation = nationRepository.selectFirst(query.builderSection());
		return AutoMapUtils.map(nation, BasicsInfoOutput.class);
	}

	private final static int PAGE_MAX_VALUE = 100;

	/**
	 * 
	 * 
	 * 2017-12-05 18:22:30
	 */
	public void callNationPageDemo() {
		IPageQueryResult<BasicsInfoOutput> result = nationPage(1, 20, (q) -> {
			q.eq("name", "张三").eq(LogicalOperatorEnum.OR, "age", "男");
		});
		if (result != null) {

		}
	}

	// 示例
	public IPageQueryResult<BasicsInfoOutput> nationPage(int currentPage, int pageSize,
			FunctionOneAction<Query> action) {
		// 复杂对象请配置xml,以下为单表分页
		Query query = new Query(Nation.class);
		if (action != null) {
			action.apply(query);
		}
		if (pageSize > PAGE_MAX_VALUE) {
			pageSize = PAGE_MAX_VALUE;
		}
		IQuerySection section = query.builderSection();
		// 统计记录数
		int count = nationRepository.countByWhere(section);
		PageQueryResult<BasicsInfoOutput> page = new PageQueryResult<BasicsInfoOutput>(currentPage, pageSize, count);
		query.page(page.getCurrentPage(), page.getPageSize()).orderBy("sortId");
		section = query.builderSection();
		if (count > 0) {
			List<Nation> entitys = nationRepository.selectList(section);
			List<BasicsInfoOutput> items = AutoMapUtils.mapList(entitys, BasicsInfoOutput.class);
			page.setItems(items);
		}
		return page;
	}

	/**
	 * 
	 */
	@Override
	public IPageQueryResult<BasicsInfoOutput> nationPage(PageQueryInput input) {
		// 复杂对象请配置xml,以下为单表分页
		if (input == null) {
			input = new PageQueryInput();
		}
		Query query = new Query(Nation.class);
		if (input.getCriterias() != null && input.getCriterias().size() > 0) {
			//query.criteria(input.getCriterias());
		}
		if (input.getPageSize() > PAGE_MAX_VALUE) {
			input.setPageSize(PAGE_MAX_VALUE);
		}

		IQuerySection section = query.builderSection();
		// 统计记录数
		int count = nationRepository.countByWhere(section);
		PageQueryResult<BasicsInfoOutput> page = new PageQueryResult<BasicsInfoOutput>(input.getCurrentPage(),
				input.getPageSize(), count);
		query.page(page.getCurrentPage(), page.getPageSize()).orderBy("sortId");
		section = query.builderSection();
		if (count > 0) {
			List<Nation> entitys = nationRepository.selectList(section);
			List<BasicsInfoOutput> items = AutoMapUtils.mapList(entitys, BasicsInfoOutput.class);
			page.setItems(items);
		}
		return page;
	}

	@Transactional(rollbackFor = DataRollbackException.class)
	@Override
	public BasicsInfoOutput addDuties() {

		Duties info = new Duties();
		info.setGmtCreate(new Date());
		info.setGmtModified(info.getGmtCreate());
		info.setIsDelete(false);
		info.setOrganizationId(1L);
		info.setName(UUID.randomUUID().toString().substring(0, 15));

		dutiesRepository.insert(info);

		return AutoMapUtils.map(info, BasicsInfoOutput.class);
	}

	@Transactional(rollbackFor = DataRollbackException.class)
	@Override
	public BasicsInfoOutput addNation() {
		Nation info = new Nation();
		info.setGmtCreate(new Date());
		info.setGmtModified(info.getGmtCreate());
		info.setIsDelete(false);
		info.setName(UUID.randomUUID().toString().substring(0, 15));
		nationRepository.insert(info);

		info = new Nation();
		info.setGmtCreate(new Date());
		info.setGmtModified(info.getGmtCreate());
		info.setIsDelete(false);
		 info.setName(UUID.randomUUID().toString());
		//info.setName(UUID.randomUUID().toString().substring(0, 15));
		nationRepository.insert(info);

		return AutoMapUtils.map(info, BasicsInfoOutput.class);
	}

	@Transactional(rollbackFor = DataRollbackException.class)
	@Override
	public BasicsInfoOutput addDutiesOrNation() {

		// 使用 ds1TransactionManager 则（dutiesRepository)生效，而
		// ds2TransactionManager
		// 则不会生效，因为 dutiesRepository 与 ds1TransactionManager 绑定
		// 反之 ds2TransactionManager 则
		// (nationRepository)生效，而ds1TransactionManager
		// 则不会生效，因为 nationRepository 与 ds2TransactionManager 绑定

		Duties info1 = new Duties();
		info1.setGmtCreate(new Date());
		info1.setGmtModified(info1.getGmtCreate());
		info1.setIsDelete(false);
		info1.setOrganizationId(1L);
		info1.setName(UUID.randomUUID().toString().substring(0, 15));

		dutiesRepository.insert(info1);

		info1 = new Duties();
		info1.setGmtCreate(new Date());
		info1.setGmtModified(info1.getGmtCreate());
		info1.setIsDelete(false);
		info1.setOrganizationId(1L);
		info1.setName(UUID.randomUUID().toString().substring(0, 15));

		dutiesRepository.insert(info1);

		// 此处会报措 ,因为长度太大
		Nation info = new Nation();
		info.setGmtCreate(new Date());
		info.setGmtModified(info.getGmtCreate());
		info.setIsDelete(false);
		info.setName(UUID.randomUUID().toString());

		nationRepository.insert(info);

		return AutoMapUtils.map(info1, BasicsInfoOutput.class);

	}
}
